package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyReportFormAddBo;
import com.ruoyi.system.bo.ZsyyReportFormQueryBo;
import com.ruoyi.system.bo.ZsyyReportFormEditBo;
import com.ruoyi.system.domain.ZsyyReportForm;
import com.ruoyi.system.mapper.ZsyyReportFormMapper;
import com.ruoyi.system.vo.ZsyyReportFormVo;
import com.ruoyi.system.service.IZsyyReportFormService;
import com.ruoyi.system.mapper.ZsyyReportFormMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 小区巡检上报Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyReportFormServiceImpl extends ServicePlusImpl<ZsyyReportFormMapper, ZsyyReportForm> implements IZsyyReportFormService {
    @Autowired
    private ZsyyReportFormMapper iZsyyReportFormMapper;

    @Override
    public ZsyyReportFormVo queryById(Integer id){
        return iZsyyReportFormMapper.getInfo(id);
    }

    @Override
    public TableDataInfo<ZsyyReportFormVo> queryPageList(ZsyyReportFormQueryBo bo) {
		Page<ZsyyReportFormVo> page  = iZsyyReportFormMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyReportFormVo> queryList(ZsyyReportFormQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyReportFormVo.class);
    }

    private LambdaQueryWrapper<ZsyyReportForm> buildQueryWrapper(ZsyyReportFormQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyReportForm> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), ZsyyReportForm::getName, bo.getName());
        lqw.like(StrUtil.isNotBlank(bo.getRemarks()), ZsyyReportForm::getRemarks, bo.getRemarks());
        lqw.eq(bo.getType() != null, ZsyyReportForm::getType, bo.getType());
        lqw.like(StrUtil.isNotBlank(bo.getPhone()), ZsyyReportForm::getPhone, bo.getPhone());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyReportFormAddBo bo) {
        ZsyyReportForm add = BeanUtil.toBean(bo, ZsyyReportForm.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyReportFormEditBo bo) {
        ZsyyReportForm update = BeanUtil.toBean(bo, ZsyyReportForm.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyReportForm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
